package com.ruoyi.kafka;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.LogFactoryService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: ruoyi
 * @description: ProduceData
 * @author: zengchen
 * @create: 2020-11-17 09:46
 **/
@Service
public class ProduceData {

    private static String url = "http://hongloumeng.5000yan.com/"; // 红楼梦

    private static volatile int currentHui = 0; // 当前章节

    private static volatile int currentDivs = 0; // 当前段落

    @Autowired
    private HongLouService hongLouService;

    public void getContentByPage(int pageNum) {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        try {
            //屏蔽日志信息
            LogFactoryService logFactoryService = new LogFactoryService();
            logFactoryService.setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
//            LogFactoryService.getFactory().
//            LogManager.getContext(LogAdapter.Log4jLog.class.getClassLoader(), false);
            //支持JavaScript
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setActiveXNative(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setTimeout(2000);
            HtmlPage rootPage = webClient.getPage(url);
            //设置一个运行JavaScript的时间
            webClient.waitForBackgroundJavaScript(2000);
            String html = rootPage.asXml();
            Document doc = Jsoup.parse(html);
            Elements paiban = doc.getElementsByClass("paiban");
            if (CollectionUtils.isNotEmpty(paiban)) {
                Elements lis = paiban.get(0).getElementsByTag("li");
                System.out.println("lis size=" + lis.size());
//                for (int i = 0; i < 1; i++) {
                Element a = lis.get(pageNum - 1).getElementsByTag("a").first();
                String href = a.attr("href");
                String title = a.wholeText().replaceAll("\r", "").replaceAll("\n", "").trim();
                System.out.println("title=" + title + ",href=" + href);
                WebClient webClientSub = new WebClient(BrowserVersion.CHROME);
                HtmlPage detailPage = webClientSub.getPage(url + href);
                String detailHtml = detailPage.asXml();
                Document detailDoc = Jsoup.parse(detailHtml);
                Element content = detailDoc.getElementsByClass("grap").first();
                Elements divs = content.getElementsByTag("div");
                for (int j = 0; j < divs.size(); j++) {
                    Element element = divs.get(j);
                    String contentText = element.wholeText().replaceAll("\r", "")
                            .replaceAll("\n", "").replaceAll("\t", "").replaceAll(" ", "");
                    if (StringUtils.isEmpty(contentText) || "&nbsp;".equals(contentText)) {
                        continue;
                    }
                    String[] split = contentText.split(",");
                    for (int i = 0; i < split.length; i++) {
                        hongLouService.send(HongLouConfiguration.HL_TOPIC, "hui" + pageNum, split[i]);
                    }
                }
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            hongLouService.closeProducer();
            webClient.close();
            System.out.println("webClient close 关闭连接");
        }
    }
}
